#include "MySystem.h"
#include <Ticker.h>
#include <ESP8266WiFi.h>
#include <ESP8266_Heweather.h>  // 天气库
#include "MyTime.h"

// 定时器
Ticker ticker_clock;
Ticker ticker_gif_pangzi;
Ticker ticker_renew_weather;
Ticker ticker_renew_time;
Ticker ticker_move_weather_txt;

// 定义WiFi网络信息
const char *ssid[] = { "RedmiK30   lrh", "MERCURY_1208" };
const char *password[] = { "22335544", "13828428308" };

// 时钟
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.aliyun.com", 28800);  // 设置NTP服务器地址和时区偏移量
CurrentTime mytime;

// 天气
String UserKey = "1644ef152e0542139a299f07c8652de1";  // 和风天气API
String Location = "101280402";                        // 查阅和风天气文档得到//深圳101280601//兴宁101280402
String Unit = "m";                                    // 公制-m/英制-i
String Lang = "zh";                                   // 语言 英文-en/中文-zh
float ROUND = 1.5;                                    // 更新间隔<分钟>实时天气API 10~20分钟更新一次

WeatherNow weatherNow;
WeatherForecast WeatherForecast;

// 函数
void system_setup() {
  my_screen_initial();

  // 联网
  Connect(ssid[0], password[0]);

  // 初始化NTP客户端
  timeClient.begin();
  my_screen_initial_next_step();
  // 延迟1s
  for (byte i = 0; i < 20; i++)
    loading(5);

  // 初始化时间
  mytime.timeInitial(timeClient);
  ticker_clock.attach(1, CurrentTime::staticRenewTime, &mytime);

  // 配置今日天气信息、天气预告
  weatherNow.config(UserKey, Location, Unit, Lang);
  WeatherForecast.config(UserKey, Location, Unit, Lang);

  my_screen_initial_next_step();
  // 延迟
  for (byte i = 0; i < 20; i++)
    loading(1);

  if (weatherNow.get()) {
    // print_weatherNow();
  }
  if (WeatherForecast.get()) {
    // print_weatherForcast();
  }
  my_screen_initial_go_done();

  // 打印城市
  show_city();

  // 50ms刷新一次太空人gif动画
  ticker_gif_pangzi.attach_ms(50, gif_pangzi_next_step);

  // 天气栏信息
  make_weather_txt();
  // 移动文本框
  ticker_move_weather_txt.attach_ms(25, weather_scroll_move);

  // 15min更新一次天气数据
  // ticker_renew_weather.once(15 , renew_weather);
  // 一天更新一次时间数据
  // ticker_renew_time.attach(24 * 60 * 60, renew_time);

  activate_popup("这是一个弹窗");
}

void renew_time() {
  if (WiFi.status() == WL_CONNECTED) {
    mytime.timeInitial(timeClient);
  }
}

void renew_weather() {
  if (WiFi.status() == WL_CONNECTED) {
    // 天气
    weatherNow.get();
    WeatherForecast.get();

    make_weather_txt();
  }
  // ticker_renew_weather.once(15 , renew_weather);
}

// 联网
void Connect(const char *inner_ssid, const char *inner_password) {
  // 连接到WiFi网络
  WiFi.begin(inner_ssid, inner_password);
  while (WiFi.status() != WL_CONNECTED) {
    for (byte i = 0; i < 10; i++)
      loading(50);
  }

  Serial.println("");
  Serial.println("WiFi connected");
}

// 打印天气检查
void print_weatherNow() {
  Serial.println(F("======Weahter Now Info======"));
  Serial.print(F("Last Update: "));
  Serial.println(weatherNow.getLastUpdate());  // 获取服务器更新天气信息时间
  Serial.print(F("Temperature: "));
  Serial.println(weatherNow.getTemp());  // 获取实况温度
  Serial.print(F("FeelsLike: "));
  Serial.println(weatherNow.getFeelLike());  // 获取实况体感温度
  Serial.print(F("Icon: "));
  Serial.println(weatherNow.getIcon());  // 获取当前天气图标代码
  Serial.print(F("Weather Now: "));
  Serial.println(weatherNow.getWeatherText());  // 获取实况天气状况的文字描述
  Serial.print(F("windDir: "));
  Serial.println(weatherNow.getWindDir());  // 获取实况风向
  Serial.print(F("WindScale: "));
  Serial.println(weatherNow.getWindScale());  // 获取实况风力等级
  Serial.print(F("Humidity: "));
  Serial.println(weatherNow.getHumidity());  // 获取实况相对湿度百分比数值
  Serial.print(F("Precip: "));
  Serial.println(weatherNow.getPrecip());  // 获取实况降水量,毫米
  Serial.println(F("========================"));
}

void print_weatherForcast() {
  for (int i = 0; i < 3; i++) {
    Serial.print(F("==========Day "));
    Serial.print(i);
    Serial.println(F("=========="));
    Serial.print("Server Response: ");
    Serial.println(WeatherForecast.getServerCode());  // 获取API状态码
    Serial.print(F("Last Update: "));
    Serial.println(WeatherForecast.getLastUpdate());  // 获取服务器更新天气信息时间
    Serial.print(F("Fx Data: "));
    Serial.println(WeatherForecast.getFxDate(i));  // 获取预测的时间
    Serial.print(F("TempMax: "));
    Serial.println(WeatherForecast.getTempMax(i));  // 获取最高温度
    Serial.print(F("TempMin: "));
    Serial.println(WeatherForecast.getTempMin(i));  // 获取最低温度
    Serial.print(F("Icon: "));
    Serial.println(WeatherForecast.getIconDay(i));  // 获取天气图标代码
    Serial.print(F("Weather Direction: "));
    Serial.println(WeatherForecast.getTextDay(i));  // 获取天气状况的文字描述
    Serial.print(F("WindDir: "));
    Serial.println(WeatherForecast.getWindDirDay(i));  // 获取风向
    Serial.print(F("WindScale: "));
    Serial.println(WeatherForecast.getwindScaleDay(i));  // 获取风力等级
    Serial.print(F("Humidity: "));
    Serial.println(WeatherForecast.getHumidity(i));  // 获取相对湿度百分比数值
    Serial.print(F("Precip: "));
    Serial.println(WeatherForecast.getPrecip(i));  // 获取降水量,毫米
    Serial.print(F("UvIndex: "));
    Serial.println(WeatherForecast.getUvIndex(i));  // 获取紫外线强度指数
    Serial.println(F("========================="));
  }
}